package com.chenzehe.zdmya.service;

import java.util.List;

import javax.annotation.Resource;
import static com.chenzehe.java.core.Status.STATUS_NORMAL;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chenzehe.java.core.orm.Page;
import com.chenzehe.java.core.orm.PageRequest;
import com.chenzehe.zdmya.dao.CouponDao;
import com.chenzehe.zdmya.domain.Coupon;

@Service
@Transactional
public class CouponService extends BaseService implements ICouponService {

	@Resource
	CouponDao couponDao;

	@Override
	public Page<Coupon> queryCoupons(PageRequest pageRequest) {
		String hql = "FROM Coupon x WHERE status=?";
		return couponDao.findPage(pageRequest, hql, STATUS_NORMAL.getState());
	}

	@Override
	public List<Coupon> queryLimitCoupons(int limit) {
		String hql = "FROM Coupon WHERE status=? ORDER BY id DESC";
		return couponDao.findLimit(hql, limit, STATUS_NORMAL.getState());
	}

	@Override
	public Coupon getById(int id) {
		Coupon coupon = couponDao.get(id);
		if (coupon.getStatus() == STATUS_NORMAL.getState()) {
			return coupon;
		}
		return null;
	}

	@Override
	public String getGoUrl(int id) {
		String hql = "SELECT new Coupon(goUrl) FROM Coupon WHERE id=?";
		List<Coupon> listCoupon = couponDao.find(hql, id);
		if (listCoupon != null && listCoupon.size() > 0) {
			return listCoupon.get(0).getGoUrl();
		}
		return "/";
	}

	@Override
	public List<Coupon> queryByMallId(int mallId) {
		String hql = "FROM Coupon WHERE mallId=?";
		return couponDao.find(hql, mallId);
	}
}
